package com.zds.passwordbox.common.util;

import android.app.Activity;
import android.app.Dialog;
import android.view.LayoutInflater;
import android.widget.TextView;

import com.zds.passwordbox.R;


public class LoadingDialogUtil {

    private static Dialog loadingDialog;

    /**
     * 显示加载对话框
     *
     * @param activity  所在的Activity上下文
     * @param message   对话框中要显示的提示信息（可为null，使用默认信息“正在加载...”）
     */
    public static void showLoadingDialog(Activity activity, String message) {
        if (activity == null || activity.isFinishing()) {
            return;
        }
        if (loadingDialog == null) {
            loadingDialog = new Dialog(activity, R.style.LoadingDialogStyle);
            LayoutInflater inflater = activity.getLayoutInflater();
            loadingDialog.setContentView(inflater.inflate(R.layout.dialog_loading, null));
            // 设置对话框不可取消（可根据需求修改为可取消）
            loadingDialog.setCancelable(false);
            // 设置对话框外部点击无效（可根据需求修改）
            loadingDialog.setCanceledOnTouchOutside(false);
        }
        if (message!= null) {
            TextView textView = loadingDialog.findViewById(R.id.textViewLoading);
            textView.setText(message);
        }
        if (!loadingDialog.isShowing()) {
            loadingDialog.show();
        }
    }

    /**
     * 隐藏加载对话框
     */
    public static void hideLoadingDialog() {
        if (loadingDialog!= null && loadingDialog.isShowing()) {
            loadingDialog.dismiss();
            loadingDialog = null;
        }
    }
}